//
//  BVddd.m
//  XiangYu
//
//  Created by YYKJ on 2017/6/30.
//  Copyright © 2017年 YYKJ. All rights reserved.
//

#import "BVAMPSearchPOIManager.h"
#import <AMapLocationKit/AMapLocationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#define DefaultLocationTimeout 10
#define DefaultReGeocodeTimeout 5

@interface BVAMPSearchPOIManager ()<AMapLocationManagerDelegate,AMapSearchDelegate>

@property (nonatomic ,strong) AMapLocationManager *locationManager;
@property (nonatomic ,strong) AMapSearchAPI       *search;
@property (nonatomic ,strong) NSMutableArray      *dataArr;

@end

@implementation BVAMPSearchPOIManager

-(id)initSearchPOI{
    
    if (self == [super init]) {
        NSLog(@"这里执行吗");
        self.dataArr = [NSMutableArray array];
        
        self.locationManager = [[AMapLocationManager alloc] init];
        [self.locationManager setDelegate:self];
        [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        [self.locationManager setLocationTimeout:DefaultLocationTimeout];
        [self.locationManager setReGeocodeTimeout:DefaultReGeocodeTimeout];
        
        self.search = [[AMapSearchAPI alloc] init];
        self.search.delegate = self;
        AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];
        request.sortrule            = 0;
        request.requireExtension    = YES;
        request.radius              = 1000;
        
        [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
            
            if (error.code == AMapLocationErrorLocateFailed)
            {
                if (self.searchFailure) {
                    self.searchFailure(@"定位失败");
                }
                return;
            }else{
                NSLog(@"这里执行不执行");
                CLLocationCoordinate2D pt =location.coordinate;
                request.location  = [AMapGeoPoint locationWithLatitude:pt.latitude longitude:pt.longitude];
                [self.search AMapPOIAroundSearch:request];
            }
        }];
    }
    return self;
   
}

- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
    NSLog(@"那这里呢 也执行吗");
    if (response.pois.count == 0)
    {
        if (self.searchFailure) {
            self.searchFailure(@"获取周边信息失败");
        }
        return;
    }
    [response.pois enumerateObjectsUsingBlock:^(AMapPOI *obj, NSUInteger idx, BOOL *stop) {
        NSLog(@"----------%@",obj.name);
        [self.dataArr addObject:obj.name];
    }];
    
    if (self.searchPOIBlock) {
        self.searchPOIBlock(self.dataArr);
    }
    
}

@end
